package com.whz.disruptor.properties;

import com.lmax.disruptor.WaitStrategy;
import com.lmax.disruptor.dsl.ProducerType;
import lombok.Data;
import org.springframework.beans.BeanUtils;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.util.ClassUtils;

/**
 * @author whz
 */
@Data
@ConfigurationProperties(prefix = "common.micro.starter.disruptor")
public class DisruptorProperties {
    /**
     * 是否启用disruptor,默认开启
     */
    private boolean enable = true;
    /**
     * disruptor 线程数
     */
    private int threadNums = Runtime.getRuntime().availableProcessors();
    /**
     * ring buffer size ,必须是2的倍数
     */
    private int ringBufferSize = 1 << 20;

    /**
     * WaitStrategy, 默认 BlockingWaitStrategy
     */
    private String WaitStrategyClass = "com.lmax.disruptor.BlockingWaitStrategy";

    /**
     * 生产者类型,默认单一生产者
     */
    private ProducerType producerType = ProducerType.SINGLE;

    public WaitStrategy getWaitStrategy() {
        try {
            Class<?> clz = ClassUtils.forName(WaitStrategyClass, DisruptorProperties.class.getClassLoader());
            return (WaitStrategy) BeanUtils.instantiateClass(clz);
        } catch (Exception exception) {
            throw new RuntimeException(exception);
        }
    }
}